{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Using TensorFlow backend.\n"
     ]
    }
   ],
   "source": [
    "from numpy import *\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "import keras\n",
    "from matplotlib import pyplot as plt\n",
    "from keras.models import Sequential\n",
    "from keras.layers import Dense, Dropout, Flatten, Conv3D, MaxPooling3D\n",
    "BATCH_SIZE = 50\n",
    "NUM_CLASSES = 2\n",
    "NUM_EPOCHS = 20"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "adress1='C:/Users/29276/Desktop/machinelearnling/train_val'\n",
    "adress2='C:/Users/29276/Desktop/machinelearnling'\n",
    "adress3='C:/Users/29276/Desktop/machinelearnling/test'\n",
    "adress4='C:/Users/29276/Desktop/machinelearnling'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(465, 100, 100, 100)"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import os\n",
    "x1 = np.load(adress1+'/candidate'+str(1)+'.npz')\n",
    "x_train=[x1 for p in range(465)]\n",
    "x_trainvoxel=[x1['voxel'] for p in range(465)]\n",
    "x_trainseg=[x1['seg'] for p in range(465)]\n",
    "j=0\n",
    "i=0\n",
    "while (i<584) :\n",
    "    if(os.path.exists(adress1+'/candidate'+str(i)+'.npz')):\n",
    "        x1 = np.load(adress1+'/candidate'+str(i)+'.npz')\n",
    "        x_train[j]=x1\n",
    "        x_trainvoxel[j]=x_train[j]['voxel']\n",
    "        x_trainseg[j]=x_train[j]['seg']\n",
    "        j+=1\n",
    "    i+=1\n",
    "x_train=x_trainvoxel*(np.array(x_trainseg).astype(int))\n",
    "np.shape(x_train)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "y_train1=pd.read_csv(adress2+'/train_val.csv')\n",
    "y_train=[1 for p in range(465)]\n",
    "for z in range(265):\n",
    "    y_train[z]=y_train1['lable'][z]\n",
    "y_train1=y_train"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(117, 100, 100, 100)"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import os\n",
    "x2 = np.load(adress3+'/candidate'+str(11)+'.npz')\n",
    "x_test=[x2 for p in range(117)]\n",
    "x_testvoxel=[x2['voxel'] for p in range(117)]\n",
    "x_testseg=[x2['seg'] for p in range(117)]\n",
    "y_testname=[' ' for p in range(117)]\n",
    "j=0\n",
    "i=0\n",
    "while (i<583) :\n",
    "    if(os.path.exists(adress3+'/candidate'+str(i)+'.npz')):\n",
    "        x2 = np.load(adress3+'/candidate'+str(i)+'.npz')\n",
    "        x_test[j]=x2\n",
    "        x_testvoxel[j]=x_test[j]['voxel']\n",
    "        x_testseg[j]=x_test[j]['seg']\n",
    "        y_testname[j]='candidate'+str(i)\n",
    "        j+=1\n",
    "    i+=1\n",
    "x_test=x_testvoxel*(np.array(x_testseg).astype(int))\n",
    "np.shape(x_test)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "x_train = x_train.astype('float32') / 255.\n",
    "x_test = x_test.astype('float32') / 255.\n",
    "x_train2 = x_train[:,30:70,30:70,30:70]\n",
    "x_test2 = x_test[:,30:70,30:70,30:70]\n",
    "y_train = keras.utils.to_categorical(y_train, NUM_CLASSES)\n",
    "#数据增强\n",
    "x_train3=x_train2\n",
    "for h in range(0,462,7):\n",
    "    #平移\n",
    "    x_train3[h,0:39,:,:]=x_train2[h,1:40,:,:]\n",
    "    x_train3[h,39,:,:]=x_train2[h,0,:,:]\n",
    "    \n",
    "    x_train3[h+1,1:40,:,:]=x_train2[h+1,0:39,:,:]\n",
    "    x_train3[h+1,0,:,:]=x_train2[h+1,39,:,:]\n",
    "    \n",
    "    x_train3[h+2,:,0:39,:]=x_train2[h+2,:,1:40,:]\n",
    "    x_train3[h+2,:,39,:]=x_train2[h+2,:,0,:]\n",
    "    \n",
    "    x_train3[h+3,:,1:40,:]=x_train2[h+3,:,0:39,:]\n",
    "    x_train3[h+3,:,0,:]=x_train2[h+3,:,39,:]\n",
    "    \n",
    "    x_train3[h+4,:,:,0:39]=x_train2[h+4,:,:,1:40]\n",
    "    x_train3[h+4,:,:,39]=x_train2[h+4,:,:,0]\n",
    "    \n",
    "    x_train3[h+5,:,:,1:40]=x_train2[h+5,:,:,0:39]\n",
    "    x_train3[h+5,:,:,0]=x_train2[h+5,:,:,39]\n",
    "    \n",
    "    noise=np.random.normal(loc=0.0, scale=0.1, size=(40,40,40))\n",
    "    x_train3[h+6,:,:,:]=x_train2[h+6,:,:,:]+ noise\n",
    "    \n",
    "#    for q in range(40):\n",
    "#        x_train3[h+7,q,:,:]=x_train2[h+7,39-q,:,:]\n",
    "#    for qq in range(40):\n",
    "#        x_train3[h+8,:,qq,:]=x_train2[h+8,:,39-qq,:]\n",
    "\n",
    "x_train4=np.concatenate((x_train2, x_train3), axis = 0)\n",
    "y_train2=np.concatenate((y_train,y_train), axis = 0)\n",
    "\n",
    "x_train2 = np.reshape(x_train2,(465,40,40,40,1))\n",
    "x_train4 = np.reshape(x_train4,(930,40,40,40,1))\n",
    "x_test2 = np.reshape(x_test2,(117,40,40,40,1))\n",
    "#input_shape = (100, 100, 100)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "scrolled": false
   },
   "outputs": [],
   "source": [
    "model=keras.models.load_model(adress4+'/my_model.h5')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "a=model.predict(x_test2)\n",
    "b=a[:,1]\n",
    "#c=[0 for p in range(117)]\n",
    "#a10=[a for p in range(10)]\n",
    "#b10=[b for p in range(10)]\n",
    "#for h in range(10):\n",
    "#    a10[h]=model.predict(x_test2)\n",
    "#    b10[h]=a10[h][:,1]\n",
    "#    c=c+b10[h]\n",
    "#c=c/10\n",
    "#dataframe = pd.DataFrame({'Id':y_testname,'Predicted':c})\n",
    "dataframe = pd.DataFrame({'Id':y_testname,'Predicted':b})\n",
    "dataframe.to_csv(adress2+'/Submission.csv',index=False,sep=',')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.7716763005780347"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from sklearn.metrics import roc_auc_score\n",
    "c=model.predict(x_train2)\n",
    "d=[0 for p in range(465)]\n",
    "for z in range(465):\n",
    "    if y_train1[z]==0:\n",
    "        d[z]=c[z][0]\n",
    "    else:\n",
    "        d[z]=c[z][1]\n",
    "roc_auc_score(y_train1,d)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
